﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushJScript.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>UrlAttribute特性</h3>
    <b>1.Url特性详解</b><br />
    <p>
        Url特性主以主4个功能：<br />
        1.Url路径重写。<br />
        2.过滤请求类型,即POST,GET等。<br />
        3.定义响应类型，即ContentType。<br />
        4.压缩模式.Deflate,Zip。<br />
    </p>
    <pre class="brush:csharp;">
    /// &lt;summary>
    /// 对控制器行为设置请求方法，响应类型，压缩方式以及请求URL的特性
    /// &lt;/summary>
    [AttributeUsage(AttributeTargets.Method,AllowMultiple=false,Inherited=false)]
    public class UrlAttribute: System.Attribute
    {
        /// &lt;summary>
        /// 控制器行为的URl设置
        /// &lt;/summary>
        /// &lt;param name="urlString">请求URL&lt;/param>
        public UrlAttribute(string urlString)
        {
            this.urlString = urlString;
            this.methodType = MethodType.NONE;
            this.contentType = "text/html; charset=utf-8";
            this.compressMode = CompressMode.None;
        }
        /// &lt;summary>
        /// 请求URL
        /// &lt;/summary>
        public string urlString { get; set; }
        /// &lt;summary>
        /// 请求的类型
        /// &lt;/summary>
        public MethodType methodType { get; set; }
        /// &lt;summary>
        /// 响应类型
        /// &lt;/summary>
        public string contentType { get; set; }
        /// &lt;summary>
        /// 压缩模式
        /// &lt;/summary>
        public CompressMode compressMode { get; set; }
    }
    </pre>
    <b>2.自定义Url特性</b>
    <pre class="brush:csharp;">
        [AttributeUsage(AttributeTargets.Method,AllowMultiple=false,Inherited=false)]
        public class GetTextAttribute : NFinal.UrlAttribute
        {
            public GetTextAttribute(string urlString):base(urlString)
            {
                this.methodType=NFinal.Http.MethodType.GET;
                this.contentType="text/plain ;chartset=utf-8";
                this.compressMode=NFinal.Http.CompressMode.None;
            }   
        }
    </pre>
    把此特性写入控制器的方法中，即可实现相关功能。<br />
    当然NFinal提供了一系列的UrlAttribute.<br />
    GetHtml,PostHtml,GetJson,PostJson,GetXml,PostXml<br />
    <b>3.使用特性</b>
    <pre class="brush:csharp;">
        public IndexController : BaseController
        {
            //Url:"/Index.php"
            [GetText("/Index.php")]
            public void Index()
            {
                this.Write("Hello World!");
            }
        }
    </pre>
</body>
</html>